package com.qingsong.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.qingsong.dto.*;
import com.qingsong.entity.*;
import com.qingsong.exception.ServiceException;
import com.qingsong.mapper.CategoryMapper;
import com.qingsong.service.CategoryService;
import com.qingsong.vo.CategorySimpleListVO;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import java.time.LocalDateTime;
import java.util.List;
import static com.qingsong.entity.table.CategoryTableDef.CATEGORY;

/**
 * 类别表 服务层实现。
 *
 * @author qingsong
 * @since 2024-12-22
 */
@Service
@CacheConfig(cacheNames = "category")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>  implements CategoryService {


    @CacheEvict(allEntries = true)
    @Override
    public boolean insert(CategoryInsertDTO dto) {
        // 标题查重
        if (QueryChain.of(mapper)
                .where(CATEGORY.TITLE.eq(dto.getTitle()))
                .exists()) {
            throw new ServiceException("类别标题已存在");
        }
        // 组装 entity 实体类
        Category category = BeanUtil.copyProperties(dto, Category.class);
        category.setCreated(LocalDateTime.now());
        category.setUpdated(LocalDateTime.now());
        return mapper.insert(category) > 0;
    }

    @Cacheable(key = "#p0",
            condition = "#p0 != null",
            unless = "#result == null")
    @Override
    public Category select(Long id) {
        return mapper.selectOneWithRelationsById(id);
    }

    @Override
    @Cacheable(key = "#root.methodName", unless = "#result == null")
    //当不涉及联表时, 可以这么写 simpleList
    public List<CategorySimpleListVO> simpleList() {
        return QueryChain.of(mapper)
                .withRelations()
                .listAs(CategorySimpleListVO.class);
    }

    @Cacheable(key = "#root.methodName + ':' + #p0.toString()",
            condition = "#p0 != null",
            unless = "#result == null")
    @Override
    public Page<Category> page(CategoryPageDTO dto) {
        QueryChain<Category> queryChain = QueryChain.of(mapper)
                .orderBy(CATEGORY.IDX.asc(), CATEGORY.ID.desc());
        // title 条件
        String title = dto.getTitle();
        if (ObjectUtil.isNotNull(title)) {
            queryChain.where(CATEGORY.TITLE.like(title));
        }
        return queryChain.withRelations()
                .page(new Page<>(dto.getPageNum(), dto.getPageSize()));
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean update(CategoryUpdateDTO dto) {
        // 标题查重
        if (QueryChain.of(mapper)
                .where(CATEGORY.TITLE.eq(dto.getTitle()))
                .and(CATEGORY.ID.ne(dto.getId()))
                .exists()) {
            throw new ServiceException("类别标题已存在");
        }
        // 组装 entity 实体类
        Category category = BeanUtil.copyProperties(dto, Category.class);
        category.setUpdated(LocalDateTime.now());
        return UpdateChain.of(category)
                .where(CATEGORY.ID.eq(category.getId()))
                .update();
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean delete(Long id) {
        return mapper.deleteById(id) > 0;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean deleteBatch(List<Long> ids) {
        return mapper.deleteBatchByIds(ids) > 0;
    }

}
